﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BMUpload.Utility;
namespace BMUpload
{
    public partial class FrmLogin : Form
    {
        DataTable dtuser;
        static bool IsActived;
        public FrmLogin()
        {
            InitializeComponent();
        }
        public FrmLogin(bool re):base()
        {
            relogin = re;
        }
        private bool relogin;
        public bool Relogin
        {
            set { relogin = value; }
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            InitCombo();
            //string strconfig = string.Empty ;
            LoadUsers();
            //strconfig = AppSettings.GetValue("AutoLogin");
            //if (strconfig != null)
            //{
            //    //勾选自动登陆
            //    checkBox2.Checked = strconfig.Equals("1");
            //    if (strconfig.Equals("1") && !relogin)
            //    {
            //        //TxtPwd.Text = DESEncrypt.Decrypt(AppSettings.GetValue("Pwd"));
            //        butOK_Click(null, new EventArgs());
            //    }
            //}

        }

        private void InitCombo()
        {
            comboBox1.Items.Add("正式版本");
            comboBox1.Items.Add("测试版本");
            comboBox1.Text = "正式版本";
        }
        private void LoadUsers()
        {
            UploadBLL p = new UploadBLL();
            dtuser = p.getUsers();

            this.CmbUserID.DataSource = dtuser;//也可以用ds，此处就是ds.Tables[0];

            this.CmbUserID.DisplayMember = "USERID"; //绑定显示成员的名字
            this.CmbUserID.ValueMember = "USERID";//绑定combox显示成员的值
            if (CmbUserID.Items.Count > 0)
            {
                this.CmbUserID.SelectedIndex = 0;
            }
            
        }
        private void butOK_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text =="正式版本")
            {
                CurrentUser.Instance.EndpointName = "CommFileServiceHttpsSoap11Endpoint";
            }
            else
            {
                CurrentUser.Instance.EndpointName = "CommFileServiceHttpSoap11Endpoint";
                CurrentUser.Instance.SoftType = "练习版";
            }

            UploadBLL ub = new UploadBLL();
            if (ub.Login(CmbUserID.Text, TxtPwd.Text))
            {
                #region //保存用户名
                string pwd ="";
                if (checkBox3.Checked)
                {
                    pwd = TxtPwd.Text;
                }
                
                if (sender !=null)
                { 
                    if (checkBox2.Checked)
                    {   AppSettings.SetValue("AutoLogin", "1"); 
                        //AppSettings.SetValue("Pwd",DESEncrypt.Encrypt(TxtPwd.Text ) );
                    }
                    else
                    {   AppSettings.SetValue("AutoLogin", "0"); 
                    }
                }
                ub.saveUser(CmbUserID.Text,pwd);
                #endregion
                this.DialogResult = DialogResult.OK;
                this.Close(); 
            }
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void butFastOK_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "正式版本")
            {
                CurrentUser.Instance.EndpointName = "CommFileServiceHttpsSoap11Endpoint";
            }
            else
            {
                CurrentUser.Instance.EndpointName = "CommFileServiceHttpSoap11Endpoint";
                CurrentUser.Instance.SoftType = "练习版";
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void CmbUserID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dtuser != null)
            {
                DataTable dt = dtuser.Copy();
                DataRow[] dr = dt.Select(" userid='" + CmbUserID.Text + "'");
                if (dr.Length > 0)
                {
                    TxtPwd.Text = dr[0]["PWD"].ToString();
                }
                else
                { TxtPwd.Text = ""; }
                //如果读出来的密码不为空，则需要勾上记住密码
                checkBox3.Checked = !string.IsNullOrWhiteSpace(TxtPwd.Text);
                
            }
        }

        private void FrmLogin_Activated(object sender, EventArgs e)
        {
            if (!IsActived)
            {
                IsActived = true;
                string strconfig = string.Empty;
                strconfig = AppSettings.GetValue("AutoLogin");
                if (strconfig != null)
                {
                    //勾选自动登陆
                    checkBox2.Checked = strconfig.Equals("1");
                    if (strconfig.Equals("1") && !relogin)
                    {
                        //TxtPwd.Text = DESEncrypt.Decrypt(AppSettings.GetValue("Pwd"));
                        butOK_Click(null, new EventArgs());
                    }
                }
                
            }

        }
    }
}
